#ifdef MBM_IMIWORLD_SUPPORT

#include "mbm_mstar_inc.h"
#include "mbm_typedefine.h"
#include "mbm_camera_adapter.h"

#include "ven_cam.h"


extern u32   miipts_ven_handle;
static Int32  g_cameraHandle = 0;  
static UInt32 g_CameraId = 0;


typedef struct
{
    UInt32 y_addr;
    UInt32 uv_addr;
    UInt32 width;
    UInt32 height;

}t_mbm_scale_param;

/*
extern ISP_SERVICE_T        s_isp_service_info;

int mbm_camera_get_data(ISP_DATA_ADDR_T addr,UInt32 width,UInt32 height)
{
	ISP_SERVICE_T *s=&s_isp_service_info;
    s->service=ISP_SERVICE_CAPTURE;         
    _ISP_ServiceSendMsg(s->service,ISP_SERVICE_START_SIG,0,0,0);
}
*/

#if 0   //c watson chen for comp

static void mbm_camera_capture_init(ISP_JPEG_PARAM_T* isp_param, t_mbm_camera_capture_param* param)
{  
    DCAM_CONTEXT_T * pContext = _GetDCAMContext();
    SENSOR_EXP_INFO_T*      sensor_info_ptr = pContext->sensor_info_ptr;
    DCAMERA_RECT_T          src_rect={0};

    SCI_TRACE_LOW("mbm_camera_capture_init buf0x%x len0x%x", param->capture_buf, param->capture_len);
      
    isp_param->input_size.w = sensor_info_ptr->sensor_mode_info[SENSOR_MODE_COMMON_INIT].width;
    isp_param->input_size.h = sensor_info_ptr->sensor_mode_info[SENSOR_MODE_COMMON_INIT].height;

    //config isp input to sensor rect
    isp_param->input_rect.x = 0x00;
    isp_param->input_rect.y = 0x00;
    isp_param->input_rect.w = isp_param->input_size.w;
    isp_param->input_rect.h = isp_param->input_size.h;

    //configure isp output size
    isp_param->output_size.w = param->capture_width;
    isp_param->output_size.h = param->capture_height;

    isp_param->skip_num=0x00;

    isp_param->capture_addr= param->capture_buf;
    isp_param->capture_buf_size = param->capture_len;

    //isp_param->get_cap_data = mbm_camera_get_data;//aync
    isp_param->get_cap_data = 0;//sync

    isp_param->sensor_data_format=ISP_DATA_YUV422;    
    isp_param->output_data_format=ISP_DATA_YUV422;

    //config to capture rect
    src_rect.x = 0x00;
    src_rect.y = 0x00;
    src_rect.w = param->capture_width;
    src_rect.h = param->capture_height;

    //adjust
    DC_AdjRect_OnDistortion(&isp_param->input_rect, &src_rect, DCAMERA_DIST_MODE_TRIMMING); 
}
#endif	


UInt32 mbm_camera_capture(t_yuv_data* data, const t_mbm_camera_capture_param* param)
{
#if 0   //c watson chen for comp
    ISP_DATA_ADDR_T pispdata;
    
    ISP_JPEG_PARAM_T isp_capture={0x00};
    
    uint16 capture_width;
    uint16 capture_height;

    mbm_camera_capture_init(&isp_capture, param);

    ISP_ServiceOpen();

    if(SCI_SUCCESS == ISP_ServiceSetCaptureParam((&isp_capture)))
    {
        if(SCI_SUCCESS != ISP_ServiceStartSyncCapture(&pispdata, &capture_width, &capture_height))
        {
            mbm_trace("ISP_ServiceStartSyncCapture error");
            ISP_ServiceClose();
            return SCI_ERROR;
        }
    }
    else
    {
        mbm_trace("ISP_ServiceSetCaptureParam error");
        ISP_ServiceClose();
        return SCI_ERROR;
    }
//	mbm_trace("cap:%d-%d\n", capture_width, capture_height);
    
    ISP_ServiceClose();

    if (data)
    {
        data->uv_addr = pispdata.uv_addr;
        data->y_addr = pispdata.y_addr;
    }
    
    return SCI_TRUE;
#else
	return 0;
#endif	
}

static void mbm_camera_cb(u32 UserData, u32 RspData)
{
    switch(UserData)
    {
    case CAM_START_PREVIEW:
        {
            ven_codec_ret_t retCode = RspData;

            if(VEN_CAM_OK == retCode)
            {
                SYS_TRACE("start preview cb success %d ", __LINE__);
            }
            else
            {
                SYS_TRACE("start preview cb failed %d ", __LINE__);
            }
        }
        break;
    case CAM_STOP_PREVIEW:
        {
            ven_codec_ret_t retCode = RspData;

            if(VEN_CAM_OK == retCode)
            {
                SYS_TRACE("stop preview cb success %d ", __LINE__);
            }
            else
            {
                SYS_TRACE("stop preview cb failed %d ", __LINE__);
            }
        }
        break;
    case CAM_GET_IMAGE:
		#if 0 //c 
        {
            ven_codec_ret_t retCode = RspData;
            SXMVC_DATACB rsb;

		 	SYS_TRACE("CAM_GET_IMAGE %d ", __LINE__);

            rsb.yuv_this = gSXMVC_ThisTDCB[2];
            rsb.yuv_that = gSXMVC_ThatTDCB[2];

            if(VEN_CAM_OK == retCode)
            {
                rsb.yuv_buffer = gSXMVC_pCamCaptureBuffer;
                rsb.yuv_size   = gSxmVc_x*gSxmVc_y;
            }
            else
            {
                rsb.yuv_buffer = NULL;
                rsb.yuv_size   = retCode;
            }

            if(gSXMVC_datareq_ind_flag)
            {
                gSXMVC_datareq_ind_flag = 0;

                if(gSXMVC_pfnTDCB[2])
                    gSXMVC_pfnTDCB[2]((void*)&rsb);	

                APPLE_SYS_LOG("CAM Capture CB Done! %d ", __LINE__);
            }
        }
	#endif
        break;
    }
}

int mbm_camera_open(void)
{
	ven_cam_ret_t ret		= VEN_CAM_OK;
	ven_req_data_t ReqData	= {0};
	ven_cam_open_param_t openParam;

	ReqData.Handle		= miipts_ven_handle;
	ReqData.bSync		= TRUE;
	ReqData.pfnCb		= mbm_camera_cb;
	ReqData.userData	= CAM_OPEN_CAMERA;
	openParam.camId = g_CameraId;
	
	openParam.mode		= VEN_CAM_CAMERA;

	ret = ven_cam_openCamera(&g_cameraHandle, &openParam, &ReqData);

	if(VEN_CAM_OK == ret)
	{
		SYS_TRACE("sync camera open success camid:%d ", g_CameraId);
	}
	else
	{
		SYS_TRACE("sync camera open failed camid:%d ", g_CameraId);
	}

	return ret;
}


int mbm_camera_close(void)
{
	ven_cam_ret_t ret = VEN_CAM_OK;
	ven_req_data_t ReqData = {0};

	ReqData.Handle		= miipts_ven_handle;
	ReqData.bSync		= TRUE;
	ReqData.pfnCb		= mbm_camera_cb;
	ReqData.userData	= CAM_CLOSE_CAMERA;

	ret = ven_cam_closeCamera(g_cameraHandle, &ReqData);

	if(VEN_CAM_OK == ret)
	{
		SYS_TRACE("async camera close success %d ", __LINE__);
	}
	else
	{
		SYS_TRACE("async camera close failed %d ", __LINE__);
	}

	g_cameraHandle = 0;

	return ret;
}
#endif
